package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.DigestUtils;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-10-25
 */
@RestController
@RequestMapping("/sys/user")
@Api(description = "用户管理")
public class SysUserController extends BaseController<ISysUserService, SysUser> {

    @ApiOperation("获取用户分页")
    @RequestMapping(value = "/getUserPage", method = RequestMethod.POST)
    public ReturnObject<List<SysUser>> getUserPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                   @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                   @ApiParam("状态（0、正常，1、禁用）") @RequestParam Integer status,
                                                   @ApiParam("配件名称") @RequestParam String name) {
        EntityWrapper<SysUser> ew = new EntityWrapper<>();
        if (status != null) {

            ew.eq("status", status);
        }
        if (StringUtils.isNotBlank(name)) {
            if (StringUtils.isNotBlank(name)) {
                ew.andNew().like("username", name).or().like("phone", name).or().like("store_name", name);
            }
        }
        ew.orderBy("crt_time", false);
        Page<SysUser> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ReturnObject<SysUser> add(@ApiParam("用户名（必填）") @RequestParam String username,
                                     @ApiParam("密码（必填）") @RequestParam String password,
                                     @ApiParam("邮箱") @RequestParam String email,
                                     @ApiParam("手机号（必填）") @RequestParam String phone) {
        SysUser info = new SysUser();
        info.setStatus(0);
        info.setUsername(username);
        info.setPassword(DigestUtils.md5Hex(password));
        info.setEmail(email);
        info.setPhone(phone);

        baseService.insert(info);
        return new ReturnObject<>(0, "保存成功", null);
    }

    @ApiOperation("查看用户详情")
    @RequestMapping(value = "/view", method = RequestMethod.POST)
    public ReturnObject<SysUser> view(@ApiParam("用户名id") @RequestParam Long id) {
        return new ReturnObject<>(0, "获取成功", baseService.selectById(id));
    }

    @IgnoreAuth
    @ApiOperation("禁用/恢复用户")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public ReturnObject<SysUser> updataStatus(@ApiParam("用户名id") @RequestParam Long id
            , @ApiParam("状态:0、正常 1、禁用") Integer status) {

        SysUser info = baseService.selectById(id);
        info.setStatus(status);
        baseService.updateById(info);
        return new ReturnObject<>(0, "获取成功", null);
    }

}

